<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" label-width="120px">
      <el-form-item label="集团id" prop="companyId">
        <el-input v-model="dataForm.companyId" placeholder="集团id"></el-input>
      </el-form-item>
      <el-form-item label="发票类型" prop="type">
        <ren-radio-group v-model="dataForm.type" dict-type="invoice_type"></ren-radio-group>
      </el-form-item>
      <el-form-item label="企业名称" prop="companyName">
        <el-input v-model="dataForm.companyName" placeholder="企业名称"></el-input>
      </el-form-item>
      <el-form-item label="税号" prop="companyTax">
        <el-input v-model="dataForm.companyTax" placeholder="税号"></el-input>
      </el-form-item>
      <el-form-item label="地址" prop="companyAddress">
        <el-input v-model="dataForm.companyAddress" placeholder="地址"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="companyTel">
        <el-input v-model="dataForm.companyTel" placeholder="电话"></el-input>
      </el-form-item>
      <el-form-item label="银行" prop="companyBank">
        <el-input v-model="dataForm.companyBank" placeholder="银行"></el-input>
      </el-form-item>
      <el-form-item label="银行账号" prop="companyBankNo">
        <el-input v-model="dataForm.companyBankNo" placeholder="银行账号"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, reactive } from "vue";
import baseService from "@/service/baseService";
import { useDebounce } from "@/utils/utils";
export default defineComponent({
  setup() {
    return reactive({
      visible: false,
      dataForm: {
        id: "",
        companyId: "",
        type: "",
        status: "",
        companyName: "",
        companyTax: "",
        companyAddress: "",
        companyTel: "",
        companyBank: "",
        companyBankNo: ""
      }
    });
  },
  computed: {
    dataRule() {
      return {
        type: [{ required: true, message: this.$t("validate.required"), trigger: "blur" }]
      };
    }
  },
  created() {
    this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataFormRef"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      baseService.get("/fii/fiiinvoice/" + this.dataForm.id).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.dataForm = res.data;
      });
    },
    // 表单提交
    dataFormSubmitHandle() {
      this.$refs["dataFormRef"].validate((valid: boolean) => {
        if (!valid) {
          return false;
        }
        (!this.dataForm.id ? baseService.post : baseService.put)("/fii/fiiinvoice", this.dataForm).then((res) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            message: this.$t("prompt.success"),
            type: "success",
            duration: 500,
            onClose: () => {
              this.visible = false;
              this.$emit("refreshDataList");
            }
          });
        });
      });
    }
  }
});
</script>
